package ch1;

/**
 * @author : zhayh
 * @date : 2022-8-17 11:55
 * @description :
 */

public class Complex {
    private double real;
    private double imag;

    public Complex() {
        this.real = 0.0;
        this.imag = 0.0;
    }

    public Complex(double real, double imag) {
        this.real = real;
        this.imag = imag;
    }

    public double getReal() {
        return this.real;
    }

    public double getImag() {
        return this.imag;
    }

    public Complex add(Complex c2) {
        return new Complex(this.real + c2.real,
                this.imag + c2.imag);
    }

    public void display() {
        System.out.println(this.real + ", " + this.imag);
    }

    public static void main(String[] args) {
        // 定义两个Complex对象
        Complex e1 = new Complex(1.2, 2.0);
        Complex e2 = new Complex(1.2, -1.0);

        // 输出e1、e1的实部和虚部
        e1.display();
        System.out.println("e1的实部：" + e1.getReal());
        System.out.println("e1的虚部：" + e1.getImag());

        // 完成e1+e2，并输出结果
        Complex e3 = e1.add(e2);
        e3.display();
    }
}
